/*
 *      Anim.cpp
 *      
 *      2009 Víctor Manuel Ochoa <kanito8a@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


#include "anim.h"

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
Anim::Anim()
{
//	m_current_animation=0;
}


/*
 * 
 * name: desconocido
 * @param
 * @return
 */
Anim::~Anim()
{

}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
bool Anim::init(Texture * _texture,unsigned short _colums_num,unsigned short _rows_num)
{
	
	if(!_texture){
		return false;
	}
	m_pLog=&Log::getInstance();
	Entity::init();
	
	m_texture=_texture;
	m_colums_num=_colums_num;
	m_rows_num=_rows_num;
	m_current_colum=0;
	m_current_row=0;
	m_anim_size=0;
	
	m_frame_colum_size=(1.0f/(float)m_colums_num);
	m_frame_row_size=(1.0f/(float)m_rows_num);
	
	m_frame_height=(float)((float)m_texture->getHeight()/m_rows_num);
	m_frame_width=(float)((float)m_texture->getWidth()/m_colums_num);
	
	Physics::setHeight(m_frame_height);
	Physics::setWidth(m_frame_width);
	
	m_frame_proportion=(float)(m_frame_width/m_frame_height);
	
	m_current_frame_num=0;
	m_frame_time=100;
	m_time.reset();
	
	m_change_anim=true;	
	m_pLog->debug("+ Anim initialiced");
	return true;
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
bool Anim::init(float _position[3],float _rotation,float _scale[3],Texture *_texture,unsigned short _colums_num,unsigned short _rows_num)
{
	if(!_texture){
		return false;
	}
	m_pLog=&Log::getInstance();
	Entity::init(_position[0],_position[1],_position[2],_rotation,_scale[0],_scale[1],_scale[2]);
	
	m_texture=_texture;
	m_colums_num=_colums_num;
	m_rows_num=_rows_num;
	m_current_colum=0;
	m_current_row=0;
	m_anim_size=0;
	
	m_frame_colum_size=(1.0f/(float)m_colums_num);
	m_frame_row_size=(1.0f/(float)m_rows_num);
	
	if(m_texture->getHeight()>0)
		m_frame_height=(float)((float)m_texture->getHeight()/m_rows_num);
		
	if(m_texture->getWidth()>0)
		m_frame_width=(float)((float)m_texture->getWidth()/m_colums_num);

	Physics::setHeight(m_frame_height);
	Physics::setWidth(m_frame_width);
	
	m_frame_proportion=(float)(m_frame_width/m_frame_height);
	
	m_current_frame_num=0;
	m_frame_time=100;
	m_time.reset();
	
	m_change_anim=true;
	
	m_pLog->debug("+ Anim' initialiced");
	return true;
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
void Anim::freeMemory()
{
	m_texture=0;
	Entity::freeMemory();
	m_pLog=0;
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
/*
void Anim::draw(){
	
	if(m_current_frame_num>=m_anim_size){
		return;
	}
	
	float coord_colum=((float)m_current_colum/m_colums_num);
	float coord_row=((float)m_current_row/m_rows_num);
	
	float *scale;
	scale=Entity::getScale();
	m_texture->bind();
	
	glColor3fv(Entity::getColor());
	
	glLoadIdentity();
	glTranslatef(0,0, 0);
	
	glEnable( GL_BLEND );
	glBlendFunc( GL_DST_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

    glBegin(GL_QUADS);
      glTexCoord2f(coord_colum, 				  	coord_row);   glVertex3f(-1.0f*m_frame_proportion*scale[0],  1.0f*scale[1], -1.0f*scale[2]);  
      glTexCoord2f((coord_colum+m_frame_colum_size),  coord_row);   glVertex3f( 1.0f*m_frame_proportion*scale[0],  1.0f*scale[1], -1.0f*scale[2]);  
      glTexCoord2f((coord_colum+m_frame_colum_size), (coord_row+m_frame_row_size));   glVertex3f( 1.0f*m_frame_proportion*scale[0], -1.0f*scale[1], -1.0f*scale[2]);  
      glTexCoord2f(coord_colum, 					 (coord_row+m_frame_row_size));   glVertex3f(-1.0f*m_frame_proportion*scale[0], -1.0f*scale[1], -1.0f*scale[2]);  
    glEnd();
	
}*/

void Anim::draw()
{	
	if(m_current_frame_num>=m_anim_size)
	{
		return;
	}
	
	float coord_colum=((float)m_current_colum/m_colums_num);
	float coord_row=((float)m_current_row/m_rows_num);
	
	float *scale;
	scale=Entity::getScale();
	m_texture->bind();
	
	glColor3fv(Entity::getColor());
		
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(getX(),getY(), 0);
	
	glEnable( GL_BLEND );
	//~ glBlendFunc( GL_DST_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND); //Enable alpha blending
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Set the blend function
	
	glScalef(getFlippedValue(), 1.0, 1.0);

    glBegin(GL_QUADS);
      glTexCoord2f(coord_colum,coord_row);   
	  glVertex2f(-(m_frame_width/2)*scale[0],-(m_frame_height/2)*scale[1]);  
	  
      glTexCoord2f((coord_colum+m_frame_colum_size),  coord_row);   
	  glVertex2f((m_frame_width/2)*scale[0],-(m_frame_height/2)*scale[1]);  
	  
      glTexCoord2f((coord_colum+m_frame_colum_size), (coord_row+m_frame_row_size));   
	  glVertex2f((m_frame_width/2)*scale[0],(m_frame_height/2)*scale[1]);  
	  
      glTexCoord2f(coord_colum,(coord_row+m_frame_row_size));   
	  glVertex2f(-(m_frame_width/2)*scale[0],(m_frame_height/2)*scale[1]);  
      
      	   
	  	//~ glTexCoord2f((coord_colum+m_frame_colum_size), (coord_row+m_frame_row_size));      
		//~ glVertex2f(-(m_frame_width/2)*scale[0],-(m_frame_height/2)*scale[1]);  
	  //~ 
	  	//~ glTexCoord2f(coord_colum,coord_row);         
	  	//~ glVertex2f((m_frame_width/2)*scale[0],-(m_frame_height/2)*scale[1]);  
	  //~ 
      //~ 
	  	//~ glTexCoord2f(coord_colum,(coord_row+m_frame_row_size));
	  	//~ glVertex2f((m_frame_width/2)*scale[0],(m_frame_height/2)*scale[1]);  
	  //~ 
		//~ 
	  	//~ glTexCoord2f((coord_colum+m_frame_colum_size),  coord_row);
		//~ glVertex2f(-(m_frame_width/2)*scale[0],(m_frame_height/2)*scale[1]);  
    glEnd();
	
	glPopMatrix();
}

/*
 * 
 * name: update
 * @param
 * @return
 */
void Anim::update()
{
	Physics::update();
	if ((m_time.currentTime()<m_frame_time || m_current_frame_num>=m_anim_size) && !m_change_anim){
		return;
	}
	
	
	m_current_colum=(m_animation[m_current_frame_num]%m_colums_num);
	m_current_row=floor(m_animation[m_current_frame_num]/m_colums_num);
	
	m_change_anim=false;
	m_time.reset();
	m_current_frame_num++;
}

/*
 * 
 * name: update
 * @param
 * @return
 */
void Anim::setAnim(std::vector<unsigned short> _current_animation,unsigned int _frame_time)
{
	m_animation=_current_animation;
	m_frame_time=_frame_time;
	m_anim_size=(unsigned short)m_animation.size();
	m_current_frame_num=0;				
	m_change_anim=true;
}

/*
 * 
 * name: update
 * @param
 * @return
 */
void Anim::setAnimationFramePoints(AnimationFramePoints _frame_points[MAX_FRAME_P0INTS])
{
	for(int i=0;i<MAX_FRAME_P0INTS;i++)
		m_frame_points[i]=_frame_points[i];

}

